package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.Bank;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  收费和保证金(银行保证金存退信息)
 */
@BusinessDataConverter(
        insertSql = "insert into nd_margin_deposit_refund_table (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, PRO_TYPE, AMOUNT_MONEY, Bank, START_DATE, EAND_DATE, REMARK, ACCOUNT_NUMBER)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE nd_margin_deposit_refund_table SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,PRO_ID = ?,PRO_TYPE = ?,AMOUNT_MONEY = ?,Bank = ?,START_DATE = ?,EAND_DATE = ?,REMARK = ?,ACCOUNT_NUMBER = ? WHERE ID = ?",
        findMigratedIdListSql = "SELECT ID FROM nd_margin_deposit_refund_table WHERE PRO_ID = ?"
)

public class NdMarginDepositRefundTable extends AbstractBusiness{
    public NdMarginDepositRefundTable(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws ScriptException, SQLException, Exception {
        List<Map> BtItemBankBailAccountList = sourceData.getBtItemBankBailAccount();
        for(Map btItemBankBailAccount:BtItemBankBailAccountList ){
            try {
                String sourceId = btItemBankBailAccount.get("INSTACCNO").toString();
                int needMigratedResult = this.needMigrated(sourceId);
                if(needMigratedResult == 0) {
                    continue;
                };
                // 单条插入或更新
                if (needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btItemBankBailAccount), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btItemBankBailAccount));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }
            } catch (Exception e) {
                throw new Exception(String.format("收费和保证金(银行保证金存退信息)[%s]迁移失败:%s\n", btItemBankBailAccount.get("ITEMNO"), e.getMessage()));
            }
    }
}

    public List buildValues (Map btItemBankBailAccount) throws SQLException, ScriptException {
        List values = new ArrayList();
        //ID	主键
        values.add(btItemBankBailAccount.get("INSTACCNO").toString());
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btItemBankBailAccount.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btItemBankBailAccount.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //PRO_ID	项目ID
        values.add(btItemBankBailAccount.get("ITEMNO"));
        //PRO_TYPE	账目类型 1是缴存  2退费
        values.add(btItemBankBailAccount.get("INSTACCTYPE").equals("1") ? "3": "4");
        //AMOUNT_MONEY	发生额|元
        values.add(btItemBankBailAccount.get("OCCURSUM"));
        //Bank	合作银行
        values.add(DictTool.getTargetDictCode(Bank.class,btItemBankBailAccount.get("BANKID")));
        //START_DATE	发生时间
        values.add(btItemBankBailAccount.get("OCCURTIME"));
        //EAND_DATE	到期时间
        values.add(btItemBankBailAccount.get("ENDTIME"));
        //REMARK	账目备注
        values.add(Tool.decodeUnicode(btItemBankBailAccount.get("INSTACCMEMO")));
        //ACCOUNT_NUMBER	账号
        values.add(btItemBankBailAccount.get("OCCURACCOUNT"));
        return values;
    }
}
